<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Http\Models\SaleModel;

class SaleController extends Controller
{
    //销售列表
    public function saleList()
    {
        $user = new SaleModel();
        $data = $user->get_list();
        $arr = [];
        foreach($data as $k)
        {
           $arr[] = $k->house_region;
        }
        $str = array_unique($arr);

        return view('admin.sale.sale_list', ['data' => $data,'str'=>$str]);
    }

    //日志列表
    public function journal()
    {
        $user = new SaleModel();
        $data = $user->get_journal();
        return view('admin.sale.journal',['data' => $data]);
    }

    //日志列表添加渲染
    public function journal_add()
    {

       return view('admin.sale.journal_add');
    }

    //日志添加
    public function add_journal(Request $Request)
    {

        $data = $Request->input();
        $res = DB::insert("insert into `king_journal` (`room_id`,`user_id`,`sale_id`,`details`,`time`,`status`,`number`) values 
        ('" . $data['room_id'] . "', '" . $data['user_id'] . "', '" . $data['sale_id'] . "', '" . $data['details'] . "', '" . $data['time'] . "', '" . $data['status'] . "', '" . $data['number'] . "')");
        if($res){
            return 1;
        }else{
            return 0;
        }

    }

    //日志删除
    public function journal_del(Request $Request)
    {
        $id = $Request->get('id');
        $res = DB::delete("delete from `king_journal` where `id`= '$id'");
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
    //日志修改渲染
    public function journal_update(Request $Request)
    {
        $id = $Request->id;
        $data = DB::table('journal')->where('id',$id)->first();
        //var_dump($data) ;die;
        return view('admin.sale.journal_update', ['data' => $data]);
    }
    //日志修改
    public function upd_journal(Request $Request)
    {
        $data = $Request->input();

        $res = DB::update("update `king_journal` set `room_id` = '".$data['room_id']."',`user_id`='".$data['user_id']."',`sale_id`='".$data['sale_id']."', `details`='".$data['details']."',`status`='".$data['status']."',`number`='".$data['number']."'
        where `id` = ?", [$data['id']]);
        if($res){
            return 1;
        }else{
            return 0;
        }
    }

    //销售列表查询
    public function search(Request $request)
    {
      /*$data = $request->input();
        $list = DB::table('house')
            ->where('house_region', $data['house_region'])
            ->orwhere('house_village_name', $data['house_village_name'])
            ->orwhere('house_price', $data['house_price'])
            ->orwhere('is_lease', $data['is_lease'])
            ->get();
      */
        $house_region = $request->input('house_region');
        $house_village_name = $request->input('house_village_name');
        $house_price = $request->input('house_price');
        $is_lease = $request->input('is_lease');
        $where = "1 = 1";
      if(!empty($house_region)){
            $where.=" and `house_region` like '%$house_region%'";
        }
       // var_dump($house_village_name);
       if(!empty($house_village_name)){
            $where.=" and `house_village_name` like '%$house_village_name%'";
        }

        if(!empty($house_price)){
    if($house_price==1){
            $where.=" and `house_price` <= 1500";
        }elseif($house_price==2)
        {
            $where.=" and `house_price` between 1500 and 2000";
        }else
        {
            $where.=" and `house_price` >= 2000";
        }
    }
        if(!empty($is_lease)){
         if($is_lease ==1)
            {
                $where.=" and `is_lease` = 1";
            }
            if($is_lease ==2){
                $where.=" and `is_lease` = 2";
            }
        }
        $list =Db::select("select * from king_house where $where");
        //dump($list);
        return view('admin.sale.search_sale', ['data' => $list]);
       // echo "<pre>";

    }
    public function search_sale()
    {
        return 111;
    }
    public function middle()
    {
        $user = new SaleModel();
        $data = $user->get_list();
        /*$arr = [];
        foreach($data as $k)
        {
            $arr[] = $k->house_region;
        }
        $str = array_unique($arr);
        dump($data);die;*/
        return view('admin.sale.sale_list', ['data' => $data]);
    }

}
